package Demo1;

public class SumCalculator {
    public static void main(String[] args) {
        int n = 5;  // 计算1到5的和
        int result = calculateSum(n);
        System.out.println("1到" + n + "的和是: " + result);

        // 测试边界情况
        testSum(0);
        testSum(10);
        testSum(-3);  // 负数情况
    }

    // 计算1到n的累加和
    public static int calculateSum(int n) {
        if (n <= 0) {
            return 0;  // 负数和0直接返回0
        }

        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;  // 累加每个数字
        }
        return sum;
    }

    // 测试不同输入的结果
    public static void testSum(int num) {
        int sum = calculateSum(num);
        System.out.println("1到" + num + "的和: " + sum);
    }
}